package com.base.vistter.iframe.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.base.vistter.iframe.entity.InverterDevices;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface InverterDevicesMapper extends BaseMapper<InverterDevices> {

    @Select({"<script>",
            "SELECT count(0) FROM inverter_devices id ",
            "LEFT JOIN ",
            "(SELECT dp, p , inverter_devices_id FROM ",
            "( SELECT *, ROW_NUMBER () OVER (PARTITION BY inverter_devices_id ORDER BY report_time DESC ) AS rn ",
            "FROM inverter_devices_power_day WHERE ",
            "report_time BETWEEN #{startTime} AND #{endTime} AND is_delete = 0 ",
            "AND inverter_station_id = #{stationId} ",
            ") sub WHERE rn = 1) today_power ",
            "ON id.id = today_power.inverter_devices_id",
            "WHERE id.is_delete = 0 ",
            " <if test='stationId != null'> AND inverter_station_id = #{stationId} </if>",
            " <if test='status != null'> AND id.`status` = 0 </if>",
    "</script>"})
    long pageInverterDevicesCount(@Param("startTime") Date startTime, @Param("endTime") Date endTime, Integer stationId, Integer status);

    @Select({"<script>",
            "SELECT id.*, today_power.dp , today_power.p FROM inverter_devices id ",
            "LEFT JOIN ",
            "(SELECT dp, p , inverter_devices_id FROM ",
            "( SELECT *, ROW_NUMBER () OVER (PARTITION BY inverter_devices_id ORDER BY report_time DESC ) AS rn ",
            "FROM inverter_devices_power_day WHERE ",
            "report_time BETWEEN #{startTime} AND #{endTime} AND is_delete = 0 ",
            "AND inverter_station_id = #{stationId} ",
            ") sub WHERE rn = 1) today_power ",
            "ON id.id = today_power.inverter_devices_id",
            "WHERE id.is_delete = 0 ",
            " <if test='stationId != null'> AND inverter_station_id = #{stationId} </if>",
            " <if test='status != null'> AND id.`status` = 0 </if>",
            "</script>"})
    List<InverterDevices> pageInverterDevicesList(@Param("startTime") Date startTime, @Param("endTime") Date endTime, Integer stationId, Integer status);
}
